/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.departement;

import com.ivanbiz.injection.CallCtx;
import com.ivanbiz.properties.SISBeanConstants;
import com.ivanbiz.properties.SISConstants;
import com.ivanbiz.properties.SISScreenConstants;
import com.ivanbiz.tools.IvanBiz;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author Ivan
 */
public class DepartementAction extends IvanBiz {

    ActionMessages actionMessages = new ActionMessages();

    public ActionForward departementList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        DepartementService departementService = (DepartementService) CallCtx.getContext(request).getBean(SISBeanConstants.DEPARTEMENT_SERVICE);
        try {
            List listDepartement = departementService.loadAll();
            request.setAttribute(SISConstants.LIST_DEPARTEMENT, listDepartement);
        } catch (Exception ex) {
            Logger.getLogger(DepartementAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.DEPARTEMENT_LIST);
    }

    public ActionForward departementAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        DepartementService departementService = (DepartementService) CallCtx.getContext(request).getBean(SISBeanConstants.DEPARTEMENT_SERVICE);
        DepartementForm departementForm = (DepartementForm) form;
        Departement departement = new Departement();
        try {
            departement.setNamaBagian(departementForm.getNamaBagian());
            departementService.saveDepartement(departement);
        } catch (Exception ex) {
            Logger.getLogger(DepartementAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.DEPARTEMENT);
    }

    public ActionForward departementEdit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        DepartementService departementService = (DepartementService) CallCtx.getContext(request).getBean(SISBeanConstants.DEPARTEMENT_SERVICE);
        DepartementForm departementForm = (DepartementForm) form;
        try {
            Departement departement = departementService.loadById(departementForm.getId());
            departementForm.setNamaBagian(departement.getNamaBagian());
        } catch (Exception ex) {
            Logger.getLogger(DepartementAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.DEPARTEMENT_EDIT);
    }

    public ActionForward departementUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        DepartementService departementService = (DepartementService) CallCtx.getContext(request).getBean(SISBeanConstants.DEPARTEMENT_SERVICE);
        DepartementForm departementForm = (DepartementForm) form;
        try {
            Departement departement = departementService.loadById(departementForm.getId());
            departement.setNamaBagian(departementForm.getNamaBagian());
            departementService.updateDepartement(departement);
        } catch (Exception ex) {
            Logger.getLogger(DepartementAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.findForward(SISScreenConstants.DEPARTEMENT_EDIT));
        }
        return mapping.findForward(SISScreenConstants.DEPARTEMENT);
    }

    public ActionForward departementDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        DepartementService departementService = (DepartementService) CallCtx.getContext(request).getBean(SISBeanConstants.DEPARTEMENT_SERVICE);
        DepartementForm departementForm = (DepartementForm) form;
        try {
            Departement departement = departementService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            departementService.deleteDepartement(departement);
        } catch (Exception ex) {
            Logger.getLogger(DepartementAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.DEPARTEMENT);
    }
}